<template>
	<view class="wrap">
		<view class="Info">
			<view class="item" @click="upImg">
				<text>头像</text>
				<image class="u-avatar" :src="avatarUrl" mode="aspectFill"></image>
				<view class="iconfont icon-dayuhao icon"></view>
			</view>
			<view class="item">
				<text>昵称</text>
				<input type="text" v-model="nickName" placeholder="未填写" maxlength="16" />
			</view>
			<view class="item">
				<text>openid</text>
				<text user-select decode>{{userInfo.openid}}</text>
				<view @click="copyText(userInfo.openid)">
					<u-icon name="cut" size="40" :color="colors"></u-icon>
				</view>
			</view>
		</view>
		<view class="randInfo" @click="getRandUser()">
			<text class="iconfont icon-suijibofang randIcon"></text>
			<text>随机头像与昵称</text>
		</view>
		<button type="primary" class="btn" :disabled="disabled" @click="updateUserInfo">保存</button>

	</view>
</template>

<script>
	let timer = null
	import * as cloudApi from '@/utils/cloudApi.js'
	export default {
		data() {
			return {
				avatarUrl: 'https://thirdwx.qlogo.cn/mmopen/vi_32/Q3auHgzwzM6ibB3G1OHp71sYFeGqPgNbv5TWic7Z6EicOkpDKfCbThqibZZLVLvA280Eez8ZT1xsO67y36br9APkZQ/132',
				nickName: 'momo',
				disabled: false,
				userInfo: {},
				colors: null,
				token: null,
				randUserList: [{
					nickName: '神经蛙',
					avatarUrl: 'https://thirdwx.qlogo.cn/mmopen/vi_32/Q3auHgzwzM6ibB3G1OHp71sYFeGqPgNbv5TWic7Z6EicOkpDKfCbThqibZZLVLvA280Eez8ZT1xsO67y36br9APkZQ/132'
				}, {
					nickName: 'momo',
					avatarUrl: 'https://thirdwx.qlogo.cn/mmopen/vi_32/Q3auHgzwzM6ibB3G1OHp71sYFeGqPgNbv5TWic7Z6EicOkpDKfCbThqibZZLVLvA280EpQFd3lbcnawk5e2rYNssdA/132'
				}, {
					nickName: '哄哄',
					avatarUrl: 'https://thirdwx.qlogo.cn/mmopen/vi_32/Q3auHgzwzM6ibB3G1OHp71sYFeGqPgNbv5TWic7Z6EicOkpDKfCbThqibZZLVLvA280Eiccz7HqL70daDHeR4TdAiatw/132'
				}, {
					nickName: '阿白',
					avatarUrl: 'https://thirdwx.qlogo.cn/mmopen/vi_32/Q3auHgzwzM6ibB3G1OHp71sYFeGqPgNbv5TWic7Z6EicOkpDKfCbThqibZZLVLvA280Eo0O7r06qgfrFCIEAibLa63w/132'
				}]
			}
		},
		onReady() {
			uni.setNavigationBarColor({
				frontColor: '#ffffff',
				backgroundColor: this.colors
			})
		},
		onLoad() {
			this.userInfo = uni.getStorageSync("userInfo") || getApp().globalData.userInfo
			this.token = uni.getStorageSync("token") || getApp().globalData.token
			this.colors = this.userInfo.themeColors || '#F3C301'

			if (this.userInfo) {
				this.nickName = this.userInfo.nickName
				this.avatarUrl = this.userInfo.avatarUrl
			}
		},
		computed: {

		},
		watch: {
			//无内容时按钮置灰
			nickName(value) {
				value ? (this.disabled = false) : (this.disabled = true);
			}
		},
		methods: {
			upImg() {
				uni.chooseImage({
					count: 1,
					success: res => {
						console.log(res.tempFiles);
						if (res.tempFilePaths.length > 0) {
							this.avatarUrl = res.tempFilePaths[0]
							//进行上传操作

							// callback方式，与promise方式二选一即可
							// uniCloud.uploadFile({
							//     filePath: filePath,
							//     cloudPath: 'a.jpg',
							//     onUploadProgress: function(progressEvent) {
							//         console.log(progressEvent);
							//         var percentCompleted = Math.round(
							//             (progressEvent.loaded * 100) / progressEvent.total
							//         );
							//     },
							//     success() {},
							//     fail() {},
							//     complete() {}
							// });

						}
					}
				});
			},
			copyText(e) {
				uni.setClipboardData({
					data: e,
					success(res) {

					}
				})
			},
			getRandUser() {
				let {
					nickName,
					avatarUrl
				} = this.randUserList[Math.floor(Math.random() * 5)]
				if (timer !== null) {
					clearTimeout(timer)
				}
				timer = setTimeout(() => {
					this.nickName = nickName
					this.avatarUrl = avatarUrl
				}, 500)

			},

			async updateUserInfo() {
				// 先删除该用户的上个头像地址，防止存储不够用
				// this.userInfo.avatarUrl
				cloudApi.call({
					name: "admin_h5",
					data: {
						api: 'delUserUrl',
						url: this.userInfo.avatarUrl
					},
					success: res => {
						console.log(res, 'userTarurl');
					}
				})
				// promise方式
				const result = await uniCloud.uploadFile({
					filePath: this.avatarUrl,
					cloudPath: this.nickName + '_avatarUrl.jpg'
				});
				console.log(result);
				uni.showLoading({
					title: '保存中',
					mask: true
				})
				if (timer !== null) {
					clearTimeout(timer)
				}
				timer = setTimeout(() => {
					cloudApi.call({
						name: "updateUserInfo",
						data: {
							api: 'updateAvatar_nicknApi',
							nickName: this.nickName,
							avatarUrl: result.fileID,
							token: this.token
						},
						success: res => {
							this.$showToast('保存成功', 'none')
							// setTimeout(() => {
							// 	uni.switchTab({
							// 		url: '/pages/me/index'
							// 	})
							// }, 1000)
						}
					})
					uni.hideLoading()
					cloudApi.getUserInfo()

				}, 500)
			},
		}
	}
</script>

<style lang="scss">
	.wrap {
		.Info {
			padding: 40rpx 0 40rpx 40rpx;

			.item {
				display: flex;
				position: relative;
				align-items: center;
				padding: 15rpx 0;
				border-bottom: 0.5rpx solid #eee;
				border-top: 0.5rpx solid #eee;
				height: 150rpx;
				color: #000000;
				font-size: 30rpx;

				image {
					width: 100rpx;
					height: 100rpx;
					// border: 5rpx solid #fff;
					border-radius: 10rpx;
				}

				text {
					margin-right: 30rpx;
				}

				.icon {
					position: absolute;
					right: 30rpx;
					color: #bbb;

				}
			}

		}

		.randInfo {
			color: skyblue;
			text-align: right;
			padding: 20rpx;
			margin-bottom: 80rpx;

			.randIcon {
				padding-right: 20rpx;
			}
		}

		.btn {
			width: 300rpx;
			height: 90rpx;
			color: #ffffff;
			border-radius: 20rpx;
			text-align: center;
		}
	}
</style>
